/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parser.part.n1;

import java.util.List;

/**
 *
 * @author Patronus
 */
public class Worker {

    String treeFormat = "List";

    public Worker() {
    }

    public Object justDoIt(List ruleTree) {
        return fillFields((Ruler) ruleTree.get(0), (List) ruleTree.get(1));
    }

    private Object fillFields(Ruler rule, List branch) {

        int position = 0;

        for (int count = 0; count < branch.size(); count++) {
            if ((count + 1) < branch.size()) {
                if (!subtree(branch.get(count)) && subtree(branch.get(count + 1))) {
                    rule.setArgs(position, fillFields((Ruler) branch.get(count), (List) branch.get(count + 1)));
                    position++;
                }
            } else if (!subtree(branch.get(count))) {

                Ruler r = (Ruler) branch.get(count);
                rule.setArgs(position, r.exequteRule());
                position++;
            }
        }
        return rule.exequteRule();
    }

    private boolean subtree(Object e) {
        return (e.getClass().getCanonicalName().indexOf(treeFormat) == -1) ? false : true;
    }
}
